import Router from 'vue-router'
import Vue from 'vue'

Vue.use(Router)

let home= ()=>import ('views/home/homeMain.vue')
let profile= ()=>import ('views/profile/profileMain.vue')
let car= ()=>import ('views/car/carMain.vue')
let category= ()=>import ('views/category/categoryMain.vue')
let detail= ()=>import ('views/data/detail.vue')
let userInfo= ()=>import('views/profile/detail/main.vue')
let userLike= ()=>import('views/profile/detail/like.vue')
let userOrder= ()=>import('views/profile/detail/order.vue')
let login= ()=>import('views/login/login.vue')
let register= ()=>import('views/login/register.vue')
let buy= ()=>import('views/buy/buy.vue')
let address= ()=>import('views/buy/address.vue')


let routes=[
  {
    path:'/',
    redirect:'/home'
  },
  {
    path:'/home',
    component:home
  },
  {
    path:'/profile',
    component:profile
  },
  {
    path:'/car',
    component:car
  },
  {
    path:'/category',
    component:category
  },
  {
    path:'/detail',
    component:detail
  },
  {
    path:'/userInfo',
    component:userInfo
  },
  {
    path:'/userLike',
    component:userLike
  },
  {
    path:'/userOrder',
    component:userOrder
  },
  {
    path:'/login',
    component:login
  },
  {
    path:'/register',
    component:register
  },
  {
    path:'/address',
    component:address
  },
  {
    path:'/buy',
    component:buy
  }
]

let router=new Router({
  routes,
  mode:'history'
})



router.beforeEach((to, from, next) => {
  let okPath=['/login','/register','/category','/detail','/home','/']
  console.log(to.path);
  if (to.path.startsWith('/login')||to.path.startsWith('/register')
        ||to.path.startsWith('/category')
        ||to.path.startsWith('/detail')
        ||to.path.startsWith('/home')
        ) {
    next()
  } else {
    let user = window.sessionStorage.getItem('user');
    if (!user) {
      next({
        path: '/login'
      })
    } else {
      next()
    }
  }
})

export default router
